const path = require('path');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

/**
 * 自定义配置
 */
require('./build/setEnv') // 设置环境变量
const config = require('./build/config')
const rules = require('./build/rules')
const plugins = require('./build/plugins')
const splitChunks = require('./build/splitChunks') // 公共模块打包

module.exports = {
    mode: process.env.NODE_ENV,
    entry: {
        'single-spa.config': './single-spa.config.js',
    },
    output: {
        publicPath: `/${config.outputDir}/`,
        filename: process.env.NODE_ENV === 'development' ? 'js/[name].js' : 'js/[name].[hash].js',
        path: path.resolve(__dirname, config.outputDir), // 打包目录
    },
    optimization: {
        // 公共模块打包提取
        splitChunks,
        minimizer: [
            new UglifyJsPlugin({
                sourceMap: process.env.SPA_ENV !== 'prod'
            }), // js压缩
            new OptimizeCSSAssetsPlugin({})
        ]
    },
    module: {
        rules
    },
    node: {
        fs: 'empty'
    },
    resolve: {
        // 自动解析确定的扩展，默认值为：[".js", ".json"]
        extensions: [".js", ".jsx", ".json", ".vue", ".css", ".less", ".scss"],
        // 设置别名
        alias: {
            '_static': path.resolve(__dirname, 'static'),
            '@': path.resolve(__dirname, 'src')
        },
        modules: [path.resolve(__dirname, 'node_modules')],
    },
    plugins,
    devtool: process.env.SPA_ENV === 'prod' ? false : 'source-map',
    externals: [],
    devServer: {
        historyApiFallback: {
            disableDotRule: true,
            index: path.join(__dirname, 'index.html'),
        }
    }
};